package com.py.warehouse.service.impl;

import java.util.List;

import com.py.warehouse.domain.WhCheckDetails;
import com.py.warehouse.mapper.WhCheckDetailsMapper;
import com.py.warehouse.mapper.WhInventoryMapper;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.py.warehouse.mapper.WhInventoryCheckMapper;
import com.py.warehouse.domain.WhInventoryCheck;
import com.py.warehouse.service.IWhInventoryCheckService;

/**
 * 盘点列表Service业务层处理
 * 
 * @author 姜威
 * @date 2024-12-04
 */
@Service
public class WhInventoryCheckServiceImpl implements IWhInventoryCheckService 
{
    @Autowired
    private WhInventoryCheckMapper whInventoryCheckMapper;

    @Autowired
    private WhCheckDetailsMapper whCheckDetailsMapper;

    @Autowired
    private WhInventoryMapper whInventoryMapper;

    /**
     * 查询盘点列表
     * 
     * @param checkId 盘点列表主键
     * @return 盘点列表
     */
    @Override
    public WhInventoryCheck selectWhInventoryCheckByCheckId(Long checkId)
    {
        return whInventoryCheckMapper.selectWhInventoryCheckByCheckId(checkId);
    }

    /**
     * 查询盘点列表列表
     * 
     * @param whInventoryCheck 盘点列表
     * @return 盘点列表
     */
    @Override
    public List<WhInventoryCheck> selectWhInventoryCheckList(WhInventoryCheck whInventoryCheck)
    {
        return whInventoryCheckMapper.selectWhInventoryCheckList(whInventoryCheck);
    }

    /**
     * 新增盘点列表
     * 
     * @param whInventoryCheck 盘点列表
     * @return 结果
     */
    @Override
    public int insertWhInventoryCheck(WhInventoryCheck whInventoryCheck)
    {
        whInventoryCheck.setCreateBy(SecurityUtils.getUsername());
        whInventoryCheck.setCreateTime(DateUtils.getNowDate());
        return whInventoryCheckMapper.insertWhInventoryCheck(whInventoryCheck);
    }

    /**
     * 修改盘点列表
     * 
     * @param whInventoryCheck 盘点列表
     * @return 结果
     */
    @Override
    public int updateWhInventoryCheck(WhInventoryCheck whInventoryCheck)
    {
        whInventoryCheck.setCreateBy(SecurityUtils.getUsername());
        whInventoryCheck.setUpdateTime(DateUtils.getNowDate());
        return whInventoryCheckMapper.updateWhInventoryCheck(whInventoryCheck);
    }

    /**
     * 批量删除盘点列表
     * 
     * @param checkIds 需要删除的盘点列表主键
     * @return 结果
     */
    @Override
    public int deleteWhInventoryCheckByCheckIds(Long[] checkIds)
    {
        return whInventoryCheckMapper.deleteWhInventoryCheckByCheckIds(checkIds);
    }

    /**
     * 删除盘点列表信息
     * 
     * @param checkId 盘点列表主键
     * @return 结果
     */
    @Override
    public int deleteWhInventoryCheckByCheckId(Long checkId)
    {
        return whInventoryCheckMapper.deleteWhInventoryCheckByCheckId(checkId);
    }

    /**
     * 修改审核状态
     * @param whInventoryCheck
     * @return
     */
    @Override
    public int updateStatus(WhInventoryCheck whInventoryCheck) {
        int result = whInventoryCheckMapper.updateStatus(whInventoryCheck);

        if (whInventoryCheck.getCheckStatus().equals("1")){
            return result;
        }
        System.out.println("进来了");
        System.out.println(whInventoryCheck);
        String code = whInventoryCheck.getCheckCode();
        System.out.println("code:" + code);

        if (result > 0){
            List<WhCheckDetails> list = whCheckDetailsMapper.selectDetailsByWarehouseCode(code);

            if (list != null){
                for (WhCheckDetails whCheckDetails : list) {
                    System.out.println("循环list");
                    System.out.println(whCheckDetails);

                    //修改盘点详情出入库数量
                    whCheckDetailsMapper.updateAlready(whCheckDetails.getDetailsId());

                    System.out.println("修改修改盘点详情出入库数量成功");

                    //减少未出入库数量
                    whInventoryMapper.updatePendingInNumJian(whCheckDetails);

                    System.out.println("修改减少未出入库数量");

                    //修改库存和金额
                    whInventoryMapper.updateCurrenInventory(whCheckDetails);

                    System.out.println("修改修改库存和金额");

                }
            }
        }

        System.out.println("修改审核成功");
        return result;
    }

}
